package com.atguigu.ssyx.common.exception;

import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created on 2023/6/26
 * 全局统一异常处理类
 */
@ControllerAdvice// Todo Aop可以再不改变代码的情况下，做增强处理
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)//处理运行时异常
    @ResponseBody//把返回的对象转换成json返回给前端
    public Result error(Exception e){
        return Result.fail(null);
    }

    //处理自定义异常
    @ExceptionHandler(SsyxException.class)
    @ResponseBody
    public Result error(SsyxException ssyxException){
        return Result.build(null, ResultCodeEnum.FAIL);//返回值为自定义返回结果的失败值
    }
}
